package cContext

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gitee.com/core/cHelper"
	"time"
)

func New() *gin.Context {
	ctx := &gin.Context{}

	traceid := CreateTraceId()
	ctx.Set("X-Trace-Id", traceid)

	return ctx
}

func CreateTraceId() string {
	now := time.Now().UnixNano()
	randInt := cHelper.RandomInt(100000, 999999)
	traceId := fmt.Sprintf("%d%d", now, randInt)

	return traceId
}

func GetTraceId(ctx *gin.Context) string {
	traceid := ctx.GetString("X-Trace-Id")

	if traceid == "" {
		traceid = CreateTraceId()
		ctx.Set("X-Trace-Id", traceid)
	}

	return traceid
}
